<?php


namespace app\admin\controller;
use think\App;
use think\facade\View;
use app\BaseController;
use think\auth\Auth;
use think\facade\Db;
use think\facade\Session;
class Index extends BaseController
{
    private $auth;

    public function __construct()
    {
        $this->auth = Auth::instance();
    }

    //后台首页
    public function index()
    {
        //管理员名字
        View::assign('user', session(config("session.session_admin"))['username']);
        return view();
    }

    //后台首页
    public function welcome()
    {
        //管理员名字
        View::assign('user', session(config("session.session_admin"))['username']);
        return view();
    }

    //修改密码
    public function password()
    {
        //管理员名字
        View::assign('user', session(config("session.session_admin"))['username']);
        return View::fetch('Index/password');
    }

    //执行修改密码
    public function ChangePassword()
    {
        //是否为post请求访问
        if(!$this->request->isPost()){
            return show(config("status.error"),'请求方式错误');
        }

        //接收新旧密码并进行合法校验
        $mes = $this->request->param('mes');
        $data = [
            'newpass' => $mes['newpass'],
            'repass' => $mes['repass'],
            'oldpass' => $mes['oldpass']
        ];
        if(md5($data['oldpass']) != session(config("session.session_admin"))['password']){
            return show (config("status.error"),'旧密码不正确');
        }
        $validate = new \app\admin\validate\UpdatePassword();
        if(!$validate->check($data)){
            return show (config("status.error"),$validate->getError());
        }
        //调用business层的AdminUser类中getstatusAttrUpdatePassword方法
        try{
            $result = \app\admin\business\AdminUser::getStatusAttrUpdatePassword($data);
        }catch (\Exception $e){
            return show (config("status.error"),$e->getMessage());
        }

        if($result){
            return show(config("status.success"),'修改成功');
        }else{
            return show(config("status.error"),'修改失败');
        }
    }


    //百度地图
    public function map()
    {
        return view();
    }

    public function match()
    {
        return view();
    }

    public function getColor()
    {
        $admin_color = Db::table('mall_admin_color')->select();
        $result = array();
        $result['code'] = 0;
        $result['msg'] = '';
        $result['data'] = $admin_color;
        return json($result);
    }

    public function updateColor()
    {
        $mall_admin_color =  Db::table('mall_admin_color')->where('status',1)->field('id')->find();
        $result = Db::table('mall_admin_color')->where('id',request()->param('id'))->field('match_css,id')->find();
        if($result['id'] == $mall_admin_color['id']){
            $admin_color = Db::table('mall_admin_color')
                ->where('id',request()->param('id'))
                ->update([request()->param('field') => request()->param('value')]);
            if($admin_color){
                Session::set('match_css',$result['match_css']);
                return '更新成功';
            }else{
                return '更新失败';
            }
        }else{
            $colcr = Db::table('mall_admin_color')->where('id',$mall_admin_color['id'])->update(['status'=>0]);
            $admin_color = Db::table('mall_admin_color')
                ->where('id',request()->param('id'))
                ->update([request()->param('field') => request()->param('value')]);
            if($admin_color){
                Session::set('match_css',$result['match_css']);
                return '更新成功';
            }else{
                return '更新失败';
            }
        }
    }
}